<?php
/**
 * Zeed Platform Project
 * Based on Zeed Framework & Zend Framework.
 *
 * LICENSE
 * http://www.zeed.com.cn/license/
 *
 * @category   Zeed
 * @package    Zeed_ChangeMe
 * @subpackage ChangeMe
 * @copyright  Copyright (c) 2010 Zeed Technologies PRC Inc. (http://www.zeed.com.cn)
 * @author     Zeed Team (http://blog.zeed.com.cn)
 * @since      2010-12-6
 * @version    SVN: $Id$
 */

class IndexController extends AdminAbstract
{

    public function index()
    {
        $this->addResult(self::RS_SUCCESS, 'json');

        /* 获取工作统计信息 */
        $count_work = $this->_getCountWork();

        /* 获取最近行动信息 */
        $recent_card = $this->_getRecentCard();

        /* 获取工作台信息 - 录像 */
        $content_wb_video = $this->_getWbVideo();

        /* 获取工作台信息 - 照片 */
        $content_wb_image = $this->_getWbImage();

        /* 获取工作台信息 - 录音 */
        $content_wb_audio = $this->_getWbAudio();

        $data = array(
            'count_work' => $count_work,
            'recent_card' => $recent_card,
            'content_wb_video' => $content_wb_video,
            'content_wb_image' => $content_wb_image,
            'content_wb_audio' => $content_wb_audio,
        );

        $this->setData('data', $data);
        if ($this->_groupid == $this->_user_group_temp) {
            $this->addResult(self::RS_SUCCESS, 'php', 'index.index.temp');
        } else {
            $this->addResult(self::RS_SUCCESS, 'php', 'index.index');
        }
        return self::RS_SUCCESS;
    }

    /**
     * 获取工作统计信息
     */
    protected function _getCountWork()
    {
        /* 计算本年度开始和结束时间 */
        $year = date('Y', time());
        $time_start = strtotime($year . '-01-01 00:00:00') * 1000;
        $time_end = strtotime($year . '-12-31 23:59:59') * 1000;

        /* 统计案件数 */
        $where_archive = array('status' => array('$in' => array(-2, 1)));
        $where_archive['$and'] = [
            ['ctime'=>
                ['$gte' => $time_start]
            ],
            ['ctime'=>
                ['$lte' => $time_end]
            ]
        ];
        $count_work['count_archive'] = Archive_Model_Content::instance()->getCount($where_archive);

        /* 统计行动数 */
        $where_card = array(
            'status' => 1,
        );
        $where_card['$and'] = [
            ['ctime'=>
                ['$gte' => $time_start]
            ],
            ['ctime'=>
                ['$lte' => $time_end]
            ]
        ];
        $cols_card = array('attachments_img', 'duration_audio', 'duration_video');
        $card_info = Archive_Model_Card::instance()->fetchByWhere($where_card, array(), null, null, $cols_card);
        $count_work['count_card'] = count($card_info);

        /* 统计录像时长、录音时长及照片张数的集合 */
        $count_duration_video_arr = $count_duration_audio_arr = $count_image = array();
        if (! empty($card_info)) {
            foreach ($card_info as $k => $v) {
                $count_duration_video_arr[] = $v['duration_video'];
                $count_duration_audio_arr[] = $v['duration_audio'];
                $count_image[] = $v['attachments_img'] ? count(explode(',', $v['attachments_img'])) : 0;
            }
        }

        /* 计算录像时长 */
        $count_work['count_duration_video'] = array_sum($count_duration_video_arr);

        /* 计算照片张数 */
        $count_work['count_image'] = array_sum($count_image);

        /* 计算录音时长 */
        $count_work['count_duration_audio'] = array_sum($count_duration_audio_arr);

        return $count_work;
    }

    /**
     * 获取最近行动信息
     */
    protected function _getRecentCard()
    {
        /* 获取行动信息 */
        $where = array(
            'status' => 1,
        );
        $order = array('ctime' => 'DESC');
        $limit = 5;
        $card = Archive_Model_Card::instance()->fetchByWhere($where, $order, $limit);

        /* 处理其他信息 */
        if (! empty($card)) {
            $archive_temp = array();
            foreach ($card as $k => &$v) {
                /* 获取案件信息 */
                if (isset($archive_temp[$v['sn']])) {
                    $v['archive_title'] = $archive_temp[$v['sn']]['title'];
                    $v['archive_id'] = $archive_temp[$v['sn']]['_id'];
                } else {
                    $archive_info = Archive_Model_Content::instance()->fetchByFV('sn', $v['sn']);
                    if (! empty($archive_info)) {
                        $v['archive_title'] = $archive_info[0]['title'];
                        $v['archive_id'] = $archive_info[0]['_id'];
                        $archive_temp[$v['sn']] = array('title' => $archive_info[0]['title'], '_id' => $archive_info[0]['_id']);
                    }
                }

                /* 处理时间 */
                $v['ctime'] = Util_Timer::formatMicrotime($v['ctime'], 'Y-m-d H:i:s');
            }
        }

        return ! empty($card) ? $card : array();
    }

    /**
     * 获取工作台信息 - 录像
     */
    protected function _getWbVideo()
    {
        /* 获取父级目录信息以及子目录信息 */
        $folders = $this->_getFirstFolders('video');
        $parent_folder = $folders['parent_folder'];
        $child_folders = $folders['child_folders'];

        /* 获取相关附件信息 */
        $where_wb_video = $where_wb_video_count = array(
            'status' => 1,
            'mediatype' => 'video',
            'userid' => $this->_userid
        );
        $where_wb_video['$and'] = $where_wb_video_count['$and'] = [
            ['pid'=>
                ['$gt' => '0']
            ]
        ];
        $where_wb_video['$and'][] = array(
            '$or' => array(
                array('folder_hid' => ''),
                array('folder_hid' => $parent_folder['hid']),
            )
        );
        $order_wb_video = array('ctime' => 'DESC');
        $contents = Attachment_Model_Content::instance()->fetchByWhere($where_wb_video, $order_wb_video);
        $count_all = Attachment_Model_Content::instance()->getCount($where_wb_video_count);

        if (! empty($contents)) {
            $card_id_temp_arr = array();
            foreach ($contents as $k => &$v) {
                /* 处理容量 */
                $v['filesize_format'] = Util_File::formatFilesize($v['filesize']);

                /* 处理时间 */
                $v['ctime'] = Util_Timer::formatMicrotime($v['ctime'], 'Y-m-d H:i:s');

                /* 处理扩展信息 */
                $v['extra'] = $v['extra'] ? json_decode($v['extra'], true) : array();

                /* 获取原文件信息 */
                $v['title_from'] = $v['title'];
                $file_from_info = Attachment_Model_Content::instance()->fetchByPK($v['pid']);
                if (! empty($file_from_info)) {
                    $v['title_from'] = $file_from_info[0]['title'];
                }

                /* 获取原文件隶属行动信息 */
                if (isset($card_id_temp_arr[$v['pid']])) {
                    $v['card_id'] = $card_id_temp_arr[$v['pid']]['_id'];
                    $v['card_title'] = $card_id_temp_arr[$v['pid']]['title'];
                    $v['sn'] = $card_id_temp_arr[$v['pid']]['sn'];
                } else {
                    $key = $v['pid'];
                    $where_card = array('attachments_video' => new MongoRegex("/$key/i"));
                    $card_info = Archive_Model_Card::instance()->fetchByWhere($where_card);
                    if (! empty($card_info)) {
                        $v['card_id'] = $card_info[0]['_id'];
                        $v['card_title'] = mb_substr($card_info[0]['title'], 0, 16) . '...';
                        $v['sn'] = $card_info[0]['sn'];
                        $card_id_temp_arr[$v['pid']] = array(
                            '_id' => $card_info[0]['_id'],
                            'title' => $card_info[0]['title'],
                            'sn' => $card_info[0]['sn']
                        );
                    }
                }

                /* 获取原文件隶属案件信息 */
                $v['archive_title'] = '';
                $archive_info = Archive_Model_Content::instance()->fetchByFV('sn', $v['sn']);
                if (! empty($archive_info)) {
                    $v['archive_title'] = mb_substr($archive_info[0]['title'], 0, 16);
                }
            }
        }

        /* 汇总返回信息 */
        $data = array(
            'parent_folder' => $parent_folder,
            'child_folders' => $child_folders,
            'contents' => $contents,
            'count_all' => $count_all,
        );

        return $data;
    }

    /**
     * 获取工作台信息 - 照片
     */
    protected function _getWbImage()
    {
        /* 获取父级目录信息以及子目录信息 */
        $folders = $this->_getFirstFolders('image');
        $parent_folder = $folders['parent_folder'];
        $child_folders = $folders['child_folders'];

        /* 获取相关附件信息 */
        $where_wb_image = $where_wb_image_count = array(
            'status' => 1,
            'mediatype' => 'image',
            'userid' => $this->_userid
        );
        $where_wb_image['$and'] = $where_wb_image_count['$and'] = [
            ['pid'=>
                ['$gt' => '0']
            ]
        ];
        $where_wb_image['$and'][] = array(
            '$or' => array(
                array('folder_hid' => ''),
                array('folder_hid' => $parent_folder['hid']),
            )
        );
        $order_wb_image = array('ctime' => 'DESC');
        $contents = Attachment_Model_Content::instance()->fetchByWhere($where_wb_image, $order_wb_image);
        $count_all = Attachment_Model_Content::instance()->getCount($where_wb_image_count);

        if (! empty($contents)) {
            $card_id_temp_arr = array();
            foreach ($contents as $k => &$v) {
                /* 处理容量 */
                $v['filesize_format'] = Util_File::formatFilesize($v['filesize']);

                /* 处理时间 */
                $v['ctime'] = Util_Timer::formatMicrotime($v['ctime'], 'Y-m-d H:i:s');

                /* 处理扩展信息 */
                $v['extra'] = $v['extra'] ? json_decode($v['extra'], true) : array();

                /* 获取原文件信息 */
                $v['wb_type'] = 'image';
                $v['title_from'] = $v['title'];
                $file_from_info = Attachment_Model_Content::instance()->fetchByPK($v['pid']);
                if (! empty($file_from_info)) {
                    if ($file_from_info[0]['mediatype'] == 'video') {
                        $v['wb_type'] = 'vod';
                    }
                    $v['title_from'] = $file_from_info[0]['title'];
                }

                /* 获取原文件所在行动 ID */
                if (isset($card_id_temp_arr[$v['pid']])) {
                    $v['card_id'] = $card_id_temp_arr[$v['pid']]['_id'];
                    $v['card_title'] = $card_id_temp_arr[$v['pid']]['title'];
                    $v['sn'] = $card_id_temp_arr[$v['pid']]['sn'];
                } else {
                    $key = $v['pid'];
                    if ($v['wb_type'] == 'vod') {
                        $where_card = array('attachments_video' => new MongoRegex("/$key/i"));
                    } else {
                        $where_card = array('attachments_img' => new MongoRegex("/$key/i"));
                    }
                    $card_info = Archive_Model_Card::instance()->fetchByWhere($where_card);
                    if (! empty($card_info)) {
                        $v['card_id'] = $card_info[0]['_id'];
                        $v['card_title'] = $card_info[0]['title'];
                        $v['sn'] = $card_info[0]['sn'];
                        $card_id_temp_arr[$v['pid']] = array(
                            '_id' => $card_info[0]['_id'],
                            'title' => $card_info[0]['title'],
                            'sn' => $card_info[0]['sn']
                        );
                    }
                }

                /* 获取原文件隶属案件信息 */
                $v['archive_title'] = '';
                $archive_info = Archive_Model_Content::instance()->fetchByFV('sn', $v['sn']);
                if (! empty($archive_info)) {
                    $v['archive_title'] = $archive_info[0]['title'];
                }
            }
        }

        /* 汇总返回信息 */
        $data = array(
            'parent_folder' => $parent_folder,
            'child_folders' => $child_folders,
            'contents' => $contents,
            'count_all' => $count_all,
        );

        return $data;
    }

    /**
     * 获取工作台信息 - 录音
     */
    protected function _getWbAudio()
    {
        /* 获取父级目录信息以及子目录信息 */
        $folders = $this->_getFirstFolders('audio');
        $parent_folder = $folders['parent_folder'];
        $child_folders = $folders['child_folders'];

        /* 获取相关附件信息 */
        $where_wb_audio = $where_wb_audio_count = array(
            'status' => 1,
            'mediatype' => 'audio',
            'userid' => $this->_userid
        );
        $where_wb_audio['$and'] = $where_wb_audio_count['$and'] = [
            ['pid'=>
                ['$gt' => '0']
            ]
        ];
        $where_wb_audio['$and'][] = array(
            '$or' => array(
                array('folder_hid' => ''),
                array('folder_hid' => $parent_folder['hid']),
            )
        );
        $order_wb_audio = array('ctime' => 'DESC');
        $contents = Attachment_Model_Content::instance()->fetchByWhere($where_wb_audio, $order_wb_audio);
        $count_all = Attachment_Model_Content::instance()->getCount($where_wb_audio_count);

        if (! empty($contents)) {
            $card_id_temp_arr = array();
            foreach ($contents as $k => &$v) {
                /* 处理容量 */
                $v['filesize_format'] = Util_File::formatFilesize($v['filesize']);

                /* 处理时间 */
                $v['ctime'] = Util_Timer::formatMicrotime($v['ctime'], 'Y-m-d H:i:s');

                /* 处理扩展信息 */
                $v['extra'] = $v['extra'] ? json_decode($v['extra'], true) : array();

                /* 获取原文件信息 */
                $v['title_from'] = $v['title'];
                $file_from_info = Attachment_Model_Content::instance()->fetchByPK($v['pid']);
                if (! empty($file_from_info)) {
                    $v['title_from'] = $file_from_info[0]['title'];
                }

                /* 获取原文件所在行动 ID */
                if (isset($card_id_temp_arr[$v['pid']])) {
                    $v['card_id'] = $card_id_temp_arr[$v['pid']]['_id'];
                    $v['card_title'] = $card_id_temp_arr[$v['pid']]['title'];
                    $v['sn'] = $card_id_temp_arr[$v['pid']]['sn'];
                } else {
                    $key = $v['pid'];
                    $where_card = array('attachments_audio' => new MongoRegex("/$key/i"));
                    $card_info = Archive_Model_Card::instance()->fetchByWhere($where_card);
                    if (! empty($card_info)) {
                        $v['card_id'] = $card_info[0]['_id'];
                        $v['card_title'] = $card_info[0]['title'];
                        $v['sn'] = $card_info[0]['sn'];
                        $card_id_temp_arr[$v['pid']] = array(
                            '_id' => $card_info[0]['_id'],
                            'title' => $card_info[0]['title'],
                            'sn' => $card_info[0]['sn']
                        );
                    }
                }

                /* 获取原文件隶属案件信息 */
                $v['archive_title'] = '';
                $archive_info = Archive_Model_Content::instance()->fetchByFV('sn', $v['sn']);
                if (! empty($archive_info)) {
                    $v['archive_title'] = $archive_info[0]['title'];
                }
            }
        }

        /* 汇总返回信息 */
        $data = array(
            'parent_folder' => $parent_folder,
            'child_folders' => $child_folders,
            'contents' => $contents,
            'count_all' => $count_all,
        );

        return $data;
    }

    /**
     * 获取根目录下的子目录信息
     * 此处的目录信息跟随登录用户，不再使用超管用户
     *
     * @param string $media_type 文件夹类型：video、image、audio
     * @return array
     */
    protected function _getFirstFolders($media_type)
    {
        /* 获取顶级目录信息 */
        $where_wb_folder = array(
            'title' => 'workbench_' . $media_type,
            'userid' => $this->_userid
        );
        $folder_info = Attachment_Model_Folder::instance()->fetchByWhere($where_wb_folder);
        if (empty($folder_info)) {
            $folder_init_info = Attachment_Folder_Workbench_Init::run($this->_userid);
            if (! empty($folder_init_info) && $folder_init_info['status'] == 0) {
                $folder_info = $folder_init_info['data'][$media_type];
            }
        } else {
            $folder_info = $folder_info[0];
        }

        /* 获取子级目录信息 */
        $child_folders = array();
        if (! empty($folder_info)) {
            $where_child_folders = array(
                'parent_id' => $folder_info['fid'],
                'userid' => $this->_userid,
                'status' => 1
            );
            $child_folders = Attachment_Model_Folder::instance()->fetchByWhere($where_child_folders);
        }

        /* 汇总返回信息 */
        $folders = array(
            'parent_folder' => $folder_info,
            'child_folders' => $child_folders
        );
        return $folders;
    }

    /**
     * 获取工作台统计数字
     */
    public function getWbCount()
    {
        $this->addResult(self::RS_SUCCESS, 'json');

        /* 获取参数 */
        $type = $this->input->post('type');

        try {
            /* 检查请求方式 */
            if (! $this->input->isPOST()) {
                throw new Zeed_Exception('请求方式错误');
            }

            /* 检查请求参数 */
            if (! $type || ($type != 'video' && $type != 'audio' && $type != 'image')) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }

            /* 组织查询条件并进行查询 */
            $where_wb_count = array(
                'status' => 1,
                'mediatype' => $type,
                'userid' => $this->_userid
            );
            $where_wb_count['$and'] = [
                ['pid' =>
                    ['$gt' => '0']
                ]
            ];
            $count = Attachment_Model_Content::instance()->getCount($where_wb_count);

            $data = array(
                'count' => $count
            );
            $this->setData('data', $data);
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('查询失败 : ' . $e->getMessage());
        }

        return self::RS_SUCCESS;
    }
}

// End ^ Native EOL ^ UTF-8